﻿using System;
using System.Linq;
using Bronze.XXTong.DAL.DatabaseSpecific;
using Bronze.XXTong.DAL.EntityClasses;
using Bronze.XXTong.DAL.HelperClasses;
using Bronze.XXTong.DAL.Linq;

namespace Bronze.XXT.BLL
{
	public class Telephone
	{
		/// <summary>
		/// 检查某个编号的共话是否存在
		/// </summary>
		/// <param name="strPhoneSerialNumber"></param>
		/// <returns></returns>
		public static bool CheckPhone(string strPhoneSerialNumber)
		{
			bool bl = false;
			using (DataAccessAdapter adapter = new DataAccessAdapter())
			{
				LinqMetaData metaData = new LinqMetaData(adapter);
				bl = metaData.PublicTelephone.Any(o => o.FPhoneSerialNumber == strPhoneSerialNumber && o.FAvailableStatus == 1);
			}
			return bl;
		}

		/// <summary>
		/// 更该话机连接状态
		/// </summary>
		/// <param name="intConnectStatus">连接状态  1.已连接  2.未连接  3.连接异常</param>
		/// <param name="strPhoneSerialNumber">公话编号</param>
		public static Guid SetPhoneConnectStatus(int intConnectStatus, string strPhoneSerialNumber)
		{
			PublicTelephoneEntity phoneEntity = null;
			Guid phoneID = Guid.Empty;
			using (DataAccessAdapter adapter = new DataAccessAdapter())
			{
				LinqMetaData metaData = new LinqMetaData(adapter);
				phoneEntity = metaData.PublicTelephone.SingleOrDefault(o => o.FPhoneSerialNumber == strPhoneSerialNumber);
				if (phoneEntity != null)
				{
					phoneEntity.FConnectStatus = intConnectStatus;
					if (intConnectStatus == 1)
						phoneEntity.FLastConnectedTime = DateTime.Now;
					phoneID = phoneEntity.FId;
					EntityCollection ec = new EntityCollection();
					ec.Add(phoneEntity);
					var logEntity = BLL.PhoneMonitorLog.CreateLogEntity(phoneEntity.FId,
					                                                    phoneEntity.FPhoneSerialNumber,
					                                                    2,
					                                                    phoneEntity.FShellStatus,
					                                                    phoneEntity.FPowerSupplyStatus==null?"0":phoneEntity.FPowerSupplyStatus.ToString(),
					                                                    phoneEntity.FPhoneSystemVersion);
					
					ec.Add(logEntity);
					adapter.SaveEntityCollection(ec);
					
				}
			}
			return phoneID;
		}

		/// <summary>
		/// 更该话机连接状态和其他信息
		/// </summary>
		/// <param name="strPhoneSerialNumber">公话编号</param>
		/// <param name="intConnectStatus">连接状态  1.已连接  2.未连接  3.连接异常</param>
		/// <param name="intShellStatus">外壳门状态</param>
		/// <param name="intPowerSupplyStatus">供电状态</param>
		/// <param name="strPhoneSystemVersion">版本信息</param>
		/// <returns></returns>
		public static Guid SetPhoneInfo(string strPhoneSerialNumber,
		                                int intConnectStatus,
		                                Nullable<int> intShellStatus,
		                                Nullable<int> intPowerSupplyStatus,
		                                string strPhoneSystemVersion)
		{
			Guid phoneID = Guid.Empty;
			using (DataAccessAdapter adapter = new DataAccessAdapter())
			{
				LinqMetaData metaData = new LinqMetaData(adapter);
				var phoneEntity = metaData.PublicTelephone.SingleOrDefault(o => o.FPhoneSerialNumber == strPhoneSerialNumber);
				if (phoneEntity != null)
				{
					phoneID = phoneEntity.FId;
					phoneEntity.FConnectStatus = intConnectStatus;
					phoneEntity.FShellStatus = intShellStatus;
					phoneEntity.FPowerSupplyStatus = intPowerSupplyStatus;
					phoneEntity.FPhoneSystemVersion = strPhoneSystemVersion;

					EntityCollection ec = new EntityCollection();

					var logEntity = BLL.PhoneMonitorLog.CreateLogEntity(phoneID,
					                                                    strPhoneSerialNumber,
					                                                    intConnectStatus,
					                                                    intShellStatus,
					                                                    intPowerSupplyStatus == null ? "" : intPowerSupplyStatus.ToString(),
					                                                    strPhoneSystemVersion);
					ec.Add(phoneEntity);
					ec.Add(logEntity);
					adapter.SaveEntityCollection(ec);
				}
			}
			return phoneID;
		}

		/// <summary>
		/// 将所有的话机设置为离线状态
		/// </summary>
		public static void SetAllPhoneOffLine()
		{
			using (DataAccessAdapter adapter = new DataAccessAdapter())
			{
				LinqMetaData metaData = new LinqMetaData(adapter);
				var list = metaData.PublicTelephone.ToList();
				EntityCollection ec = new EntityCollection();
				list.ForEach(o =>
				             {
				             	o.FConnectStatus = 2;
				             	o.FLastConnectedTime = DateTime.Now;
				             	ec.Add(o);
				             	//记录话机离线日志
				             	var logEntity = BLL.PhoneMonitorLog.CreateLogEntity
				             		(o.FId,
				             		 o.FPhoneSerialNumber,
				             		 2,
				             		 o.FShellStatus,
				             		 o.FPowerSupplyStatus == null ?
				             		 string.Empty : o.FPowerSupplyStatus.ToString(),
				             		 o.FPhoneSystemVersion);
				             	ec.Add(logEntity);
				             });
				adapter.SaveEntityCollection(ec);
			}
		}
	}
}
